package com.wocai.jrt.orgadmin.investor.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.inject.Inject;
import javax.inject.Named;

import org.apache.commons.lang3.StringUtils;
import com.vteba.service.generic.impl.MyBatisServiceImpl;
import com.vteba.tx.jdbc.mybatis.spi.BaseDao;
import com.vteba.utils.common.PropsUtils;
import com.vteba.utils.id.ObjectId;
import com.wocai.jrt.admin.common.bean.Icon;
import com.wocai.jrt.admin.common.bean.State;
import com.wocai.jrt.orgadmin.investor.bean.EmpBreifBean;
import com.wocai.jrt.orgadmin.investor.bean.EmpGroupTreeBean;
import com.wocai.jrt.orgadmin.investor.bean.GroupBean;
import com.wocai.jrt.orgadmin.investor.bean.InvestorGroupBean;
import com.wocai.jrt.orgadmin.investor.bean.InvestorReqBean;
import com.wocai.jrt.orgadmin.investor.bean.InvestorResBean;
import com.wocai.jrt.orgadmin.investor.bean.MatchReqBean;
import com.wocai.jrt.orgadmin.investor.bean.RelationReqBean;
import com.wocai.jrt.orgadmin.investor.bean.RelationResBean;
import com.wocai.jrt.orgadmin.investor.dao.InvestorDao;
import com.wocai.jrt.orgadmin.investor.model.Investor;
import com.wocai.jrt.orgadmin.investor.model.InvestorBean;
import com.wocai.jrt.orgadmin.investor.service.spi.InvestorService;
import com.wocai.jrt.orgadmin.stats.bean.GroupOdersReqBean;
import com.wocai.jrt.orgadmin.stats.bean.IndexStatsReqBean;

/**
 * 投资人相关的service业务实现。
 * 
 * @author yinlei
 * @date 2015-8-4 20:08:33
 */
@Named
public class InvestorServiceImpl extends MyBatisServiceImpl<Investor, InvestorBean, String>
		implements InvestorService {
	private InvestorDao investorDao;
	
	public static final String FILE_PATH = PropsUtils.get("file.path")+"evaluafile/";
	@Override
	@Inject
	public void setBaseDao(BaseDao<Investor, InvestorBean, String> investorDao) {
		this.baseDao = investorDao;
		this.investorDao = (InvestorDao) investorDao;
	}

	/**
	 * 按检索条件查询投资人
	 * 
	 * @param InvestorReqBean
	 *            reqBean
	 * @return List<Investor>
	 */
	@Override
	public List<Investor> queryFactor(InvestorReqBean reqBean) {
		List<Investor> list = investorDao.queryFactor(reqBean);
		return list;
	}

	/**
	 * 获取投资人分组列表
	 * 
	 * @return List<GroupBean>
	 */
	@Override
	public List<GroupBean> getGroupList(GroupBean groupBean) {
		List<GroupBean> list = investorDao.getGroupList(groupBean);
		return list;
	}

	/**
	 * 获取投资人列表
	 * 
	 * @param Investor
	 *            model
	 * @return List<Investor>
	 */
	@Override
	public List<InvestorResBean> queryMyList(Investor model) {
		List<InvestorResBean> list = investorDao.queryMyList(model);
		return list;
	}

	/**
	 * 获取联系记录列表
	 * 
	 * @param Investor
	 *            model
	 * @return List<RelationResBean>
	 */
	@Override
	public List<RelationResBean> queryRelation(Investor model) {
		List<RelationResBean> list = investorDao.queryRelation(model);
		return list;
	}

	/**
	 * 获取投资人所在分组列表
	 * 
	 * @param String
	 *            id
	 * @return List<GroupBean>
	 */
	@Override
	public List<GroupBean> getInvestorGroupList(Investor v) {
		List<GroupBean> list = investorDao.getInvestorGroupList(v);
		return list;
	}

	/**
	 * 获取投资人所不在在分组列表
	 * 
	 * @param String
	 *            id
	 * @return List<GroupBean>
	 */
	@Override
	public List<GroupBean> getOutGroupList(Investor v) {
		List<GroupBean> list = investorDao.getOutGroupList(v);
		return list;
	}

	/**
	 * 获取机构下所有理财师
	 * 
	 * @param String
	 *            id
	 * @return List<GroupBean>
	 */
	@Override
	public List<EmpBreifBean> getOrgEmployeeList(String tenantIdentifier, String employeeKey) {
		MatchReqBean mBean = new MatchReqBean();
		mBean.setEmployeeKey(employeeKey);
		mBean.setOrgId(tenantIdentifier);
		List<EmpBreifBean> list = investorDao.getOrgEmployeeList(mBean);
		return list;
	}

	/**
	 * 给投资人分配理财师
	 * 
	 * @param MatchReqBean
	 *            matchReqBean
	 * @return boolean
	 */
	@Override
	public boolean allotEmp(MatchReqBean matchReqBean) {
		boolean bool = investorDao.allotEmp(matchReqBean);
		return bool;
	}

	private List<EmpGroupTreeBean> empGroupBeans;

	/**
	 * 获取机构下理财师团队tree
	 * 
	 * @param String
	 *            orgId
	 * @return List<EmpGroupTreeBean>
	 */
	@Override
	public EmpGroupTreeBean empTree(String orgId) {
		EmpGroupTreeBean root = new EmpGroupTreeBean();
		try {
			// 获取机构下所有理财师列表
			empGroupBeans = investorDao.getEmpGroupList(orgId);
			root.setId("-1");
			root.setText("理财师团队");
			root.setChildren(recursiveTree(orgId));
			State state = new State();
			state.setOpened(true);
			state.setLoaded(true);
			root.setState(state);
			root.setIcon(Icon.FILE_DANGER);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return root;
	}

	/**
	 * 递归算法解析成树形结构
	 * 
	 * @param id
	 * @return List<EmpGroupTreeBean>
	 */
	public List<EmpGroupTreeBean> recursiveTree(String id) {
		// 查询所有root节点
		List<EmpGroupTreeBean> childTreeNodes = queryTreeNode(id);
		// 遍历所有root节点下的子节点
		for (EmpGroupTreeBean child : childTreeNodes) {
			List<EmpGroupTreeBean> childlist = recursiveTree(child.getId());
			child.setChildren(childlist);
			State state = new State();
			state.setOpened(true);
			state.setLoaded(true);
			child.setState(state);
			child.setIcon(Icon.FILE_DANGER);
		}
		return childTreeNodes;
	}

	public List<EmpGroupTreeBean> queryTreeNode(String id) {
		if (StringUtils.isBlank(id)) {
			return Collections.emptyList();
		}
		List<EmpGroupTreeBean> empList = new ArrayList<EmpGroupTreeBean>();
		for (EmpGroupTreeBean emp : empGroupBeans) {
			String parent = emp.getParent();
			if (id.equals(parent)) {
				empList.add(emp);
			}
		}
		return empList;
	}

	/**
	 * 统计全查询的行数
	 * 
	 * @param model
	 */
	@Override
	public long countQueryBy(Investor model) {
		long l = investorDao.countQueryBy(model);
		return l;
	}

	/**
	 * 修改投资人分组
	 * 
	 * @param String
	 *            groupName
	 */
	@Override
	public int modifyGroup(Investor model) {
		int result = 0;
		// 投资人当前所在分组
		List<GroupBean> list = investorDao.getInvestorGroupList(model);
		// 删除投资人现有分组
		investorDao.delInvestorGroup(model);
		// 投资人当前所在分组Set
		Set<String> setList = new HashSet<String>();
		// 需要更新的投资人分组Set
		Set<String> updateSet = new HashSet<String>();
		for (GroupBean s : list) {
			setList.add(s.getId());
		}
		// 添加投资人新分组
		String groupString = model.getGroupId();
		List<InvestorGroupBean> gBeans = new ArrayList<InvestorGroupBean>();
		Set<String> set = new HashSet<String>();
		if (groupString != null && !"".equals(groupString)) {
			String[] str = groupString.split(",");
			for (String string : str) {
				set.add(string);
				InvestorGroupBean gBean = new InvestorGroupBean();
				gBean.setId(ObjectId.get().toString());
				gBean.setGroupId(string);
				gBean.setInvestorId(model.getId());
				gBeans.add(gBean);
			}
		}
		if (gBeans.size() > 0) {
			// 批量插入投资人分组信息
			result = investorDao.addInvestorGroup(gBeans);
		}
		// 将分组人数发生变动的分组插入updateSet中
		for (String str : set) {
			if (!setList.contains(str)) {
				updateSet.add(str);
			}
		}
		for (String str : setList) {
			if (!set.contains(str)) {
				updateSet.add(str);
			}
		}
		if (updateSet.size() > 0) {
			// 统计投资人分组人数
			for (String string : updateSet) {
				investorDao.updateInvestorGroupNum(string);
			}
		}
		return result;
	}

	/**
	 * 新增联系记录
	 * 
	 * @param RelationReqBean
	 *            relationReqBean
	 * @return int
	 */
	@Override
	public int addRela(RelationReqBean relationReqBean) {
		int result = investorDao.addRela(relationReqBean);
		return result;
	}

	/**
	 * 新增投资人分组
	 * 
	 * @param GroupBean
	 *            gBean
	 * @return int
	 */
	@Override
	public int addGroup(GroupBean gBean) {
		int result = investorDao.addGroup(gBean);
		return result;
	}

	/**
	 * 删除投资人分组
	 * 
	 * @param GroupBean
	 *            gBean
	 * @return JsonBean
	 */
	@Override
	public int delGroup(GroupBean gBean) {
		int result = investorDao.delGroup(gBean);
		return result;
	}

	/**
	 * 重新分配投资人所属分组、重新统计投资人分组人数
	 * 
	 * @param String
	 *            id, String[] gStrings
	 * @return int
	 */
	@Override
	public int saveGroupId(String id, Set<String> gStrings) {
		// 新分组set gStrings
		// 投资人当前所在分组
		Investor v = new Investor();
		v.setId(id);
		List<GroupBean> list = investorDao.getInvestorGroupList(v);
		Set<String> setList = new HashSet<String>();
		for (GroupBean s : list) {
			setList.add(s.getId());
		}
		// 需要新插入的分组
		Set<String> gNewList = new HashSet<String>();
		// 需要删除的分组
		Set<String> gDelList = new HashSet<String>();
		for (String str : gStrings) {
			if (!setList.contains(str)) {
				gNewList.add(str);
			}
		}
		for (String str : setList) {
			if (!gStrings.contains(str)) {
				gDelList.add(str);
			}
		}
		// 插入新的投资人分组
		if (gNewList.size() > 0) {
			List<InvestorGroupBean> listNew = new ArrayList<InvestorGroupBean>();
			for (String s : gNewList) {
				InvestorGroupBean gbBean = new InvestorGroupBean();
				gbBean.setId(ObjectId.get().toString());
				gbBean.setGroupId(s);
				gbBean.setInvestorId(id);
				listNew.add(gbBean);
			}
		}
		// 删除投资人分组
		if (gDelList.size() > 0) {
			List<InvestorGroupBean> listDel = new ArrayList<InvestorGroupBean>();
			for (String s : gDelList) {
				InvestorGroupBean gbBean = new InvestorGroupBean();
				gbBean.setGroupId(s);
				gbBean.setInvestorId(id);
				listDel.add(gbBean);
			}
		}
		return 0;
	}

	/**
	 * 修改投资人分组名
	 * 
	 * @param GroupBean
	 *            gBean
	 * @return JsonBean
	 */
	@Override
	public int modifyGroup(GroupBean gBean) {
		return investorDao.modifyGroup(gBean);
	}
	
	 /**
     * 修改投资人信息
     * @param Investor model
     * @return int
     */
	@Override
	public int updateInvestor(Investor model) {
		return investorDao.updateInvestor(model);
	}

	@Override
	public List<Investor> queryGroupInvestorPerformance(GroupOdersReqBean groupOdersReqBean) {
		return investorDao.queryGroupInvestorPerformance(groupOdersReqBean);
	}

	@Override
	public int yestodayCount(IndexStatsReqBean indexStatsReqBean) {
		return investorDao.yestodayCount(indexStatsReqBean);
	}

	@Override
	public int turnCount(IndexStatsReqBean indexStatsReqBean) {
		return investorDao.turnCount(indexStatsReqBean);
	}
	
}
